# -*-  coding: UTF-8  -*-
import db, cfg
import calendar, datetime
import wx

__DB = db.ActivityDB('activities.db3')

class Bilan(object) :
    def __init__(self, activity, nb) :
        self.activity = activity
        self.nb       = nb

def MustAsk(dt) :
   """Return (date, time) list of must have been filled activities"""

   d, t = db.DT2Sql(dt)
   when = []

   # Get the index of the current time
   w = cfg.GetWhen()[:]
   w.append(t)
   idx = sorted(w).index(t)

   last = __DB.GetLastActivity()
   if not last :
       # Still no activity
       if idx != 0 and dt.weekday() in range(5) :
           # Ask for the first ones
           when = [(d, i) for i in range(idx)]

   else :
       # There are some activities

       # Create the available activities
       currentDate = db.Sql2DT(__DB.GetFirstDate())
       availableActivities = []
       while currentDate <= datetime.datetime.today() :
           if currentDate.weekday() in range(5) :
               if currentDate.date() == datetime.date.today() :
                   availableActivities.extend( [(db.DT2Sql(currentDate)[0], i) for i in range(idx)] )
               else :
                   availableActivities.extend( [(db.DT2Sql(currentDate)[0], i) for i in range(len(cfg.GetWhen()))] )
           currentDate += datetime.timedelta(1)

       # Get all activities date / time
       existingActivities = [(a.date, a.when) for a in __DB.GetAllActivities()]

       # Remove the activities that already exists
       when = [act for act in availableActivities if act not in existingActivities]

   return when

def AddActivity(d, t, activity) :
   assert isinstance(t, int)
   bInserted = False
   try :
       __DB.InsertActivity(d, t, activity)
       bInserted = True
   except db.ActivitiesError, e :
       wx.MessageBox(str(e))
   return bInserted

def GetActivitiesByDay(d) :
   return __DB.GetByDate(d)

def GetActivitiesByMonth(d) :
   dt = db.Sql2DT(d)
   d1 = dt.date().replace(day = 1)
   d2 = dt.date().replace(day = calendar.monthrange(dt.year, dt.month)[1])
   return __DB.GetBetween(d1, d2)

def __GetBilanBetween(d1, d2) :
   activities = [a.activity for a in __DB.GetBetween(str(d1), str(d2))]
   return [Bilan(a, activities.count(a)) for a in set(activities)]

def GetBilanByMonth(d) :
   dt = db.Sql2DT(d)
   d1 = dt.date().replace(day = 1)
   d2 = dt.date().replace(day = calendar.monthrange(dt.year, dt.month)[1])
   return __GetBilanBetween(d1, d2)

def GetBilanByYear(d) :
   dt = db.Sql2DT(d)
   d1 = dt.date().replace(day =  1, month =  1)
   d2 = dt.date().replace(day = 31, month = 12)
   return __GetBilanBetween(d1, d2)

def GetAllActivitiesName() :
   return __DB.GetAllActivitiesName()

def GetAllHolidays() :
   return __DB.GetAllHolidays()

def InsertHoliday(d) :
   try :
       __DB.InsertHoliday(d)
   except db.ActivitiesError, e :
       wx.MessageBox(e)

def RemoveHoliday(h) :
   try :
       __DB.RemoveHoliday(h)
   except db.ActivityError, e :
       wx.MessageBox(e)

def Archive(filename, fromDate, toDate, delActivities) :
   print "ActivityMgr::Archive(%s, %s, %s, %s)" % (filename, fromDate, toDate, delActivities)
